package com.woniuxy.exception;

import com.woniuxy.dto.DtoExceptionInfo;
import com.woniuxy.dto.DtoJSONResult;
import com.woniuxy.dto.DtoJSONResultCode;
import com.woniuxy.util.ExceptionMsgSplit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName GlobalExceptionHandler
 * @Description 全局异常控制器
 * @Author Joker
 * @Version 1.0.0
 **/


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * @Description
     * @Author Joker
     * @Date 20:53 2021/5/12
     * @Param []
     * @Return com.woniuxy.dto.JSONResult 全局异常处理返回JSONResult
     */
    @ExceptionHandler(Exception.class)
    public DtoJSONResult exceptionHandler(Exception e){
        log.error("error:{}",e.getMessage());
        return new DtoJSONResult(DtoJSONResultCode.NONERROR.getCode(),e.getMessage(),null,null);
    }
    /**
     * @Author Joker
     * @Description MyException的父类异常
     * @Date  
     * @Param [e]
     * @Return com.woniuxy.dto.JSONResult
     **/
    @ExceptionHandler(MyException.class)
    public DtoJSONResult myExceptionHandler(MyException e){

        DtoExceptionInfo ei = ExceptionMsgSplit.msgSplit(e.getMessage());
        log.error("error:{}",ei.getMessage());
        return new DtoJSONResult(ei.getExceptionCode(),ei.getMessage(),null,null);
    }
    /**
    *@author 王维鑫
    *@Description Token异常处理
    *@Date 14:49 2021/5/13 0013
    *@Param
    *@Return
    **/
    @ExceptionHandler(TokenException.class)
    public DtoJSONResult TokenException(TokenException e){

        DtoExceptionInfo et = ExceptionMsgSplit.msgSplit(e.getMessage());
        log.error("error:{}",et.getMessage());
        return new DtoJSONResult(DtoJSONResultCode.TOKENERROR.getCode(), DtoJSONResultCode.TOKENERROR.getMessage(), null,null);
    }

    /**
     *@Author 王昊波
     *@Description 权限不足异常处理
     *@Param []
     *@return com.woniuxy.dto.DtoJSONResult
     */
    @ExceptionHandler(AuthorizationException.class)
    public DtoJSONResult handlerAuthorizationException(){
        return new DtoJSONResult(DtoJSONResultCode.AUTHORITYERROR.getCode(),DtoJSONResultCode.AUTHORITYERROR.getMessage(),null,null);
    }
}
